
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091116 - aha42 - Starts in line change tracking.
// 091121 - aha42 - Added project page link (google code) to header comment.
// 091126 - aha42 - @SuppressWarnings("serial").
// 091208 - aha42 - Renamed from FlickrException and extends new 
//                  FlickrException.
// 091215 - aha42 - FlickrResponseException(FlickrObject).
//

package org.aha.mf4j.response;

import org.aha.mf4j.FlickrException;

/**
 * <p>
 *   {@link FlickrException} for Flickr error response messages.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
@SuppressWarnings("serial")
public final class FlickrResponseException extends FlickrException 
{
  private final String m_errCode;
  
  private final String m_msg;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param errFo Parsed result representing XML element {@code err} from 
   *              Flickr.
   * @throws IllegalArgumentException If 
   *         {@code errFo.getType.equals("err")==false}.
   */
  public FlickrResponseException(FlickrObject errFo)
  {
    this(errFo.getAttribute("code"), errFo.getAttribute("msg"));
    
    if (!errFo.getType().equals("err"))
    {
      throw new IllegalArgumentException(
        "errFo does not represents an err XML element");
    }
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param errCode {@link #getErrorCode()}.
   * @param msg     {@link #getFlickrMessage()}.
   */
  public FlickrResponseException(String errCode, String msg)
  {
    super(msg+" (error code : "+errCode+")");
    m_errCode=errCode;
    m_msg=msg;
  }
  
  /**
   * <p>
   *   Gets Flickr error code.
   * </p>
   * @return Code.
   */
  public String getErrorCode(){ return m_errCode; }
  
  /**
   * <p>
   *   Gets error message from Flickr.
   * </p>
   * @return Message.
   */
  public String getFlickrMessage(){ return m_msg; }
  
}
